package com.yunxi.service.localService.impl.sto;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yunxi.bizToHub.pojo.VehicleDeliveryOrderPostingVO;
import com.yunxi.bizToHub.pojo.VehicleStockStatusVO;
import com.yunxi.bizToHub.service.VehicleStockManageDubboService;
import com.yunxi.core.base.Constant;
import com.yunxi.core.page.PageHelperPlus;
import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.core.util.BeanTools;
import com.yunxi.core.web.DictConstant;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.dao.sto.*;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.model.order.OrderVehicle;
import com.yunxi.model.order.PmsVehicleStock;
import com.yunxi.model.sto.*;
import com.yunxi.order.service.OrderOemManagerDubboService;
import com.yunxi.service.localService.CodeCreateService;
import com.yunxi.service.localService.OrderVehicleService;
import com.yunxi.service.localService.sto.*;
import com.yunxi.sto.pojo.*;
import com.yunxi.sto.service.VehiclePdiBillDubboService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 车辆出库计划单 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-05-20
 */
@Service
public class StoStockOutPlanServiceImpl extends ServiceImplPlus<StoStockOutPlanMapper, StoStockOutPlan> implements StoStockOutPlanService {


    @Autowired
    @Qualifier(value = "stoManageServiceImpl")
    StoManageService stoManageService;

    @Autowired
    @Qualifier(value = "stoStockOutServiceImpl")
    StoStockOutService stoStockOutService;

    @Autowired
    @Qualifier(value = "vehicleOfflineServiceImpl")
    VehicleOfflineService vehicleOfflineService;

    @Autowired
    @Qualifier(value = "stoVehicleLendOrderServiceImpl")
    StoVehicleLendOrderService stoVehicleLendOrderService;

    @Autowired
    @Qualifier(value = "stoVehicleDeliveryServiceImpl")
    StoVehicleDeliveryService stoVehicleDeliveryService;

    @Autowired
    @Qualifier(value = "vehicleStockManageDubboServiceImplBiz")
    VehicleStockManageDubboService vehicleStockManageDubboService;

    @Autowired
    @Qualifier(value = "codeCreateServiceImpl")
    CodeCreateService codeCreateService;

    @Autowired
    @Qualifier(value = "vehicleStockMServiceImpl")
    VehicleStockMService vehicleStockMService;

    @Autowired
    @Qualifier(value = "orderOemManagerDubboServiceImpl")
    OrderOemManagerDubboService orderOemManagerDubboService;

    @Autowired
    @Qualifier(value = "stoStockInPlanServiceImpl")
    StoStockInPlanService stoStockInPlanService;

    @Autowired
    @Qualifier(value = "stoVrfidHistoryServiceImpl")
    StoVrfidHistoryService stoVrfidHistoryService;

    @Autowired
    @Qualifier(value = "stoTransportOrderServiceImpl")
    StoTransportOrderService stoTransportOrderService;

    @Autowired
    @Qualifier(value = "orderVehicleServiceImpl")
    OrderVehicleService orderVehicleService;

    @Autowired
    @Qualifier(value = "stoTransportPlanServiceImpl")
    StoTransportPlanService stoTransportPlanService;

    @Autowired
    VehiclePdiBillDubboService vehiclePdiBillDubboService;

    private static final Logger logger = LoggerFactory.getLogger(StoStockOutPlanServiceImpl.class);

    /**
     * 作废/恢复
     * @param list
     * @param isDel
     * @throws Exception
     */
    @Override
    public void delAndReset(List<String> list, Integer isDel,String userId) throws Exception {
        if(list != null && list.size() > 0){
            for(String id : list){
                StoStockOutPlan stoStockOutPlan = baseMapper.selectById(id);
                if(stoStockOutPlan != null){
                    String billState = stoStockOutPlan.getBillState();
                    if(isDel == 1){
                        if(billState != null && !billState.equals("1")){
                            throw new Exception("操作失败,只能作废新建的单据!");
                        }
                        stoStockOutPlan.setBillState("5");
                    }
                    else if(isDel== 0){
                        if(billState != null && !billState.equals("5")){
                            throw new Exception("操作失败,只能恢复作废的单据!");
                        }
                        stoStockOutPlan.setBillState("1");
                    }
                    stoStockOutPlan.setUpdateBy(userId);
                    baseMapper.updateById(stoStockOutPlan);

                    //解冻车辆
                    QueryWrapper<VehicleOffline> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("VIN",stoStockOutPlan.getVin());
                    VehicleOffline vehicleOffline = vehicleOfflineService.getOne(queryWrapper);
                    if(vehicleOffline != null ){
                        vehicleOffline.setVehicleIsFrozen(0);
                        vehicleOffline.setIsLock(0);
                        vehicleOfflineService.saveOrUpdate(vehicleOffline);
                    }
                    QueryWrapper<PmsVehicleStock> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("VIN",stoStockOutPlan.getVin());
                    PmsVehicleStock vehicleStock = vehicleStockMService.getOne(queryWrapper1);
                    if(vehicleStock != null){
                        vehicleStock.setVehicleIsFrozen("0");
                        vehicleStockMService.saveOrUpdate(vehicleStock);
                        //新增解冻历史
                        vehicleStockMService.newFrozenHis("0",null,stoStockOutPlan.getVin(),2);
                    }
                }
                else{
                    throw new Exception("操作失败,无法根据主键id:"+id+"找到对应出库计划!");
                }
            }
        }
        else{
            throw new Exception("操作失败,list为空!");
        }
    }

    @Override
    public void print(String stoStockOutPlanId) {
        StoStockOutPlan stoStockOutPlan = new StoStockOutPlan();
        stoStockOutPlan.setStockOutPlanId(stoStockOutPlanId);
        stoStockOutPlan.setIsPrint(1);
        baseMapper.updateById(stoStockOutPlan);
    }

    @Override
    public List vinList(PartnerVO vo) {
        return baseMapper.vinList(vo);
    }

    @Override
    public void replace(StoStockOutPlanReplaceVO stoStockOutPlanReplaceVO,String userId) throws Exception{
        StoStockOutPlan stoStockOutPlan = baseMapper.selectById(stoStockOutPlanReplaceVO.getStockOutPlanId());
        String newVin = stoStockOutPlanReplaceVO.getVin();
        String oldVin = null;
        if(stoStockOutPlan != null){
            oldVin = stoStockOutPlan.getVin();
            stoStockOutPlan.setVin(stoStockOutPlanReplaceVO.getVin());
            QueryWrapper<PmsVehicleStock> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("VIN",stoStockOutPlanReplaceVO.getVin());
            PmsVehicleStock one = vehicleStockMService.getOne(queryWrapper);
            if(one != null){
                String storageLocation = one.getStorageLocation();//库位代码
                stoStockOutPlan.setStorageCode(storageLocation);
            }
            stoStockOutPlan.setUpdateBy(userId);
            //通过VIN码查询批售单付款类型,现金则合格证随车,非现金则合格证不随车
            QueryWrapper<OrderVehicle> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("VIN",newVin);
            List<OrderVehicle> list = orderVehicleService.list(queryWrapper2);
            Integer isOnVehicle = null;
            if(list != null && list .size() > 0){
                OrderVehicle orderVehicle = list.get(0);
                String payway = orderVehicle.getPayway();
                if(com.alibaba.cloud.context.utils.StringUtils.isEmpty(payway) || !payway.equals("A")){
                    isOnVehicle = 0;
                }
                else if(payway.equals("A")){
                    isOnVehicle = 1;
                }
            }
            if(isOnVehicle != null){
                stoStockOutPlan.setIsOnVehicle(isOnVehicle);
            }
            baseMapper.updateById(stoStockOutPlan);

            //冻结新vin
            lockVin(newVin,1);
            //解冻旧vin
            if(oldVin != null && !oldVin.equals(newVin)){
                lockVin(oldVin,0);
            }
        }
        else{
            throw new Exception("操作失败,无法根据主键id:"+stoStockOutPlanReplaceVO.getStockOutPlanId()+"找到对应出库单据!");
        }
    }

    private void lockVin(String newVin,Integer isLock) {
        QueryWrapper<VehicleOffline> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("VIN",newVin);
        VehicleOffline vehicleOffline = vehicleOfflineService.getOne(queryWrapper1);
        if(vehicleOffline != null){
            vehicleOffline.setVehicleIsFrozen(isLock);
            vehicleOffline.setIsLock(isLock);
            vehicleOfflineService.saveOrUpdate(vehicleOffline);
        }
    }

    /**
     * 开始出库
     * @param stoStockScanVO
     * @throws Exception
     */
    @Override
    public void beginStockOut(StoStockScanVO stoStockScanVO) throws Exception {
        String vin = stoStockScanVO.getVin();
        StoStockOutPlan stoStockOutPlan = getStockOutOrderByVin(vin);
        //校验计划出库日期与当前日期是否匹配
        Date planStockOutDate = stoStockOutPlan.getPlanStockOutDate();
//        stoStockInPlanService.checkPlanStockInDate(planStockOutDate,DictConstant.STOCK_OUT);
        stoStockOutPlan.setBillState("3");//准备出库
        baseMapper.updateById(stoStockOutPlan);
    }

    /**
     * 完成出库
     * 1.变更出库计划单单据状态,将RFID绑定到出库计划单
     * 2.释放车辆占用库位
     * 3.生成出库单
     * @param stoStockScanVO
     */
    @Override
    public void complete(StoStockScanVO stoStockScanVO, LoginInfo loginInfo) throws Exception{
        //1.更改出库计划单据状态
        String rfidNo = stoStockScanVO.getRfidNo();
        String vin = stoStockScanVO.getVin();
        StoStockOutPlan stoStockOutPlan = getStockOutOrderByVin(vin);
        stoStockOutPlan.setBillState("4");//出库完成
        stoStockOutPlan.setStockOutDriver(stoStockScanVO.getRfidNo());
        stoStockOutPlan.setStockOutDate(new Date());
        baseMapper.updateById(stoStockOutPlan);

        //2.释放仓库使用状态
        updateChangeStatus(stoStockOutPlan);

        //3.生成出库单
        newStockOutOrder(stoStockOutPlan);

        //4.更改车辆主数据库存状态为出库
        changeVehicleStatus(vin,DictConstant.VEHICLE_OUT_STOCK,DictConstant.VEHICLE_TURN_STATE_11VDCCK);

        //5.如果出库类型为借车出库/消耗领用,则需要根据VIN+单据状态为车辆未出库
        // 找到唯一的一张借车单,更新单据状态为车辆已出库
        if(stoStockOutPlan.getStockOutType() != null && (stoStockOutPlan.getStockOutType().equals("4") || stoStockOutPlan.getStockOutType().equals("13"))){
            changeLendOrderStatus(vin,1,2,rfidNo,null);
        }

        //6.整车交货过账接口,接口向SAP推送交货过账信息
        pushDeliveryInfoToSap(vin);

        //7.车辆出入库状态接口,PMS到AIEG
        vehicleStockStatusToAIEG(vin);

        //8.出库推送信息到订单
        orderOemManagerDubboService.updateOrderVehicleStatusByVin(vin, Constant.PMS_ORDER_VEHICLE_CHECK_STATUS_WAIT_RECEIVE);

        //9.更新车辆库存状态为出库
        updateStockStatus(vin);

        //10.新增车辆RFID解绑历史
        newBindHistory(loginInfo, vin);

        //11.刷新运单发运时间
        updateTransportOrder(stoStockOutPlan);

        //解冻车辆
        unFrozenVehicle(vin);

        //12.回写自提车辆发运计划的提车时间
        updateTransportPlan(stoStockOutPlan);

        //13.VDC出库完成推送APP
        vehiclePdiBillDubboService.sentMsgToApp(vin,DictConstant.APP_RPNAME_VDC_OUT,DictConstant.APP_CODE_03);
        //发运在途推送到APP
        vehiclePdiBillDubboService.sentMsgToApp(vin,DictConstant.APP_RPNAME_VDC_TRANS,DictConstant.APP_CODE_04);

    }

    /**
     * 更新自提车辆发运计划
     * @param stoStockOutPlan
     */
    private void updateTransportPlan(StoStockOutPlan stoStockOutPlan) {
        QueryWrapper<StoTransportPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("VIN",stoStockOutPlan.getVin());//VIN码
        queryWrapper.eq("VBELN",stoStockOutPlan.getDeliveryNo());//SAP交货单号
        queryWrapper.eq("IS_PICKED",0);//未自提
        List<StoTransportPlan> list = stoTransportPlanService.list(queryWrapper);
        if(list != null && list.size() > 0){
            for (StoTransportPlan bo : list){
                bo.setIsPicked(1);
                bo.setPickDate(new Date());
                stoTransportPlanService.saveOrUpdate(bo);
            }
        }
    }

    /**
     * 更新车辆库存状态为出库
     * @param vin
     */
    private void updateStockStatus(String vin) {
        QueryWrapper<PmsVehicleStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("VIN",vin);
        PmsVehicleStock one = vehicleStockMService.getOne(queryWrapper);
        if(one != null){
            one.setStorageStatus(DictConstant.VEHICLE_OUT_STOCK);
            one.setOutStockDate(new Date());//出库日期
            vehicleStockMService.updateById(one);
        }
    }

    /**
     * 根据VIN码获取出库计划
     * @param vin
     * @return
     * @throws Exception
     */
    private StoStockOutPlan getStockOutOrderByVin(String vin) throws Exception {
        QueryWrapper<StoStockOutPlan> queryWrapper = new QueryWrapper();
        queryWrapper.eq("VIN",vin);
        queryWrapper.ne("BILL_STATE","4");
        queryWrapper.ne("BILL_STATE","5");
        StoStockOutPlan stoStockOutPlan = baseMapper.selectOne(queryWrapper);
        if(stoStockOutPlan == null || stoStockOutPlan.getStockOutPlanId() == null){
            throw new Exception("操作失败,无法根据车辆VIN码:"+vin+"找到出库计划单!");
        }
        return stoStockOutPlan;
    }

    /**
     * 车辆出入库状态接口到AIEG
     * @param vin
     */
    private void vehicleStockStatusToAIEG(String vin) {
        VehicleStockStatusVO vehicleStockStatusVO = new VehicleStockStatusVO();
        vehicleStockStatusVO.setVin(vin);
        vehicleStockStatusVO.setType(DictConstant.STOCK_OUT);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        vehicleStockStatusVO.setOperationDate(format);
        vehicleStockManageDubboService.vehicleStockStatusPms2Tsp(vehicleStockStatusVO);
    }

    /**
     * 接口向SAP推送交货过账信息
     * @param vin
     */
    private void pushDeliveryInfoToSap(String vin) {
        VehicleDeliveryOrderPostingVO vehicleDeliveryOrderPostingVO = new VehicleDeliveryOrderPostingVO();
        QueryWrapper<StoVehicleDelivery> deliveryQuery = new QueryWrapper();
        deliveryQuery.eq("VHVIN",vin);
        StoVehicleDelivery stoVehicleDelivery = stoVehicleDeliveryService.getOne(deliveryQuery);
        if(stoVehicleDelivery != null){
            BeanTools.copyProperties(stoVehicleDelivery,vehicleDeliveryOrderPostingVO);
            vehicleDeliveryOrderPostingVO.setXblnr(stoVehicleDelivery.getVbeln());//外向交货单号
        }
        vehicleDeliveryOrderPostingVO.setLfimg("1");//数量
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = simpleDateFormat.format(new Date());
        vehicleDeliveryOrderPostingVO.setBudat(date);//移库日期
        vehicleDeliveryOrderPostingVO.setBwart(DictConstant.DELIVERY_OUT);//移库类型 出库
        vehicleDeliveryOrderPostingVO.setVhvin(vin);
        String matnrCode = vehicleOfflineService.getMatnrCodeByVin(vin);
        vehicleDeliveryOrderPostingVO.setMatnr(matnrCode);
        vehicleStockManageDubboService.vehicleDeliveryOrderPostingPms2Sap(vehicleDeliveryOrderPostingVO);
    }

    /**
     * 更新借车单据状态
     * @param vin
     * @param orderState
     */
    public void changeLendOrderStatus(String vin,Integer oldOrderState ,Integer orderState,String outRfidNo,String inRfidNo) {
        QueryWrapper<StoVehicleLendOrder> queryWrapper3 = new QueryWrapper();
        queryWrapper3.eq("VIN", vin);
        queryWrapper3.eq("ORDER_STATE", oldOrderState);
        StoVehicleLendOrder stoVehicleLendOrder = stoVehicleLendOrderService.getOne(queryWrapper3);
        if (stoVehicleLendOrder != null) {
            stoVehicleLendOrder.setOrderState(orderState);
            if(StringUtils.isNotEmpty(outRfidNo)){
                stoVehicleLendOrder.setOutDriver(outRfidNo);//出库司机RFID
            }
            if(StringUtils.isNotEmpty(inRfidNo)){
                stoVehicleLendOrder.setInDriver(inRfidNo);//入库司机RFID
                stoVehicleLendOrder.setActualReturnDate(new Date());//实际归还日期
            }
            stoVehicleLendOrderService.updateById(stoVehicleLendOrder);

            //解冻车辆
            QueryWrapper<VehicleOffline> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("VIN",stoVehicleLendOrder.getVin());
            VehicleOffline vehicleOffline2 = vehicleOfflineService.getOne(queryWrapper2);
            if(vehicleOffline2 != null){
                vehicleOffline2.setVehicleIsFrozen(0);
                vehicleOffline2.setIsLock(0);
                vehicleOfflineService.saveOrUpdate(vehicleOffline2);

                QueryWrapper<PmsVehicleStock> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("VIN",vehicleOffline2.getVin());
                PmsVehicleStock vehicleStock = vehicleStockMService.getOne(queryWrapper);
                if(vehicleStock != null){
                    vehicleStock.setVehicleIsFrozen("0");
                    vehicleStockMService.saveOrUpdate(vehicleStock);
                }

                //新增车辆解冻历史
                vehicleStockMService.newFrozenHis("0",null,vehicleOffline2.getVin(),2);
            }
        }
    }

    /**
     * 更改车辆主数据库存状态为出库
     * @param vin
     * @param storageStatus
     */
    public void changeVehicleStatus(String vin,Integer storageStatus,String carState) {
        QueryWrapper<VehicleOffline> queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("VIN",vin);
        VehicleOffline vehicleOffline = vehicleOfflineService.getOne(queryWrapper2);
        if(vehicleOffline != null){
            // 10VDCRK 不修改状态
            if(!DictConstant.VEHICLE_TURN_STATE_10VDCRK.equals(carState)){
            vehicleOffline.setVehicleIsFrozen(0);//未冻结
            vehicleOffline.setIsLock(0);//未锁定
            }
            vehicleOffline.setFrozenReasonKey(null);
            vehicleOffline.setStorageStatus(storageStatus);//库存状态改为出库
            vehicleOffline.setCarState(carState);//流转状态改为VDC出库
            vehicleOffline.setVehicleRfid(null);//出库即清空车辆RFID
            vehicleOfflineService.updateById(vehicleOffline);
        }
    }

    @Override
    public List<StoStockOutPlanVO> export(StoStockOutPlanVO stoStockOutPlanVO) throws Exception {
        Map map = BeanTools.bean2map(stoStockOutPlanVO);
        return baseMapper.queryByList(map);
    }

    @Override
    public PageInfo<StoStockOutPlanLateVO> lateStockOutPlan(StoStockOutPlanLateVO stoStockOutPlanLateVO, Integer pageNum, Integer pageSize) throws Exception {
        PageHelperPlus.startPage(pageNum,pageSize);
        List<StoStockOutPlanLateVO> list = baseMapper.lateStockOutPlan(stoStockOutPlanLateVO);
        PageInfo<StoStockOutPlanLateVO> pageInfo = new PageInfo<StoStockOutPlanLateVO>(list);
        return pageInfo;
    }

    /**
     * 获取今日上班司机
     * @param stoStorageStaffDriverVO
     * @return
     * @throws Exception
     */
    @Override
    public List<StoStorageStaffDriverVO> getDriver(StoStorageStaffDriverVO stoStorageStaffDriverVO) throws Exception {
        return baseMapper.getDriver(stoStorageStaffDriverVO);
    }

    /**
     * 根据VIN码查询出库计划
     * @param vin
     * @return
     */
    @Override
    public List<StoStockOutPlanVO> planListByVin(String vin,String rfid) {
        StoStockScanVO stoStockScanVO = new StoStockScanVO();
        stoStockScanVO.setVin(vin);
        stoStockScanVO.setRfidNo(rfid);
        return baseMapper.planListByVin(stoStockScanVO);
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @Override
    public StoStockOutPlanVO getDetail(String id) {
        return baseMapper.getDetail(id);
    }

    /**
     * 生成出库单
     * @param stoStockOutPlan
     */
    private void newStockOutOrder(StoStockOutPlan stoStockOutPlan) {
        StoStockOut stoStockOut = new StoStockOut();
        StoStockOutVO stoStockOutVO = baseMapper.queryStockOutInfo(stoStockOutPlan.getStockOutPlanNo());
        BeanTools.copyProperties(stoStockOutPlan,stoStockOut);
        if(stoStockOutVO != null){
            stoStockOut.setAnnoucementNumber(stoStockOutVO.getAnnoucementNumber());
            stoStockOut.setMotorCode(stoStockOutVO.getMotorCode());
        }
        String so = codeCreateService.createCode("SO", 4);
        stoStockOut.setStockOutNo(so);
        stoStockOut.setStockOutPlanNo(stoStockOutPlan.getStockOutPlanNo());
        stoStockOut.setStockOutDate(new Date());
        if(stoStockOut.getStorageCode() != null){
            String s = stoManageService.queryStroageByCode(stoStockOut.getStorageCode());
            stoStockOut.setStorageLocationCode(s);//仓库代码
        }
        stoStockOutService.saveOrUpdate(stoStockOut);
    }

    /**
     * 释放仓库使用状态
     * @param stoStockOutPlan
     */
    private void updateChangeStatus(StoStockOutPlan stoStockOutPlan) {
        String storageCode = stoStockOutPlan.getStorageCode();
        QueryWrapper<StoManage> queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("STORAGE_CODE",storageCode);
        StoManage stoManage = stoManageService.getOne(queryWrapper1);
        if(stoManage != null){
            stoManage.setStoragePositionState("1");
            stoManageService.updateById(stoManage);
        }
    }

    /**
     * 新增车辆RFID解绑历史
     * @param loginInfo
     * @param vin
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    private void newBindHistory(LoginInfo loginInfo, String vin) throws IllegalAccessException, InvocationTargetException {
        StoVrfidHistory stoVrfidHistory = new StoVrfidHistory();
        stoVrfidHistory.setPartnerCode(loginInfo.getPartnerCode());
        stoVrfidHistory.setVin(vin);
        QueryWrapper<VehicleOffline> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("VIN",vin);
        VehicleOffline vehicleOffline = vehicleOfflineService.getOne(queryWrapper);
        if(vehicleOffline != null){
            stoVrfidHistory.setVehicleRfid(vehicleOffline.getVehicleRfid());
        }
        stoVrfidHistory.setBindType(2);//解绑
        stoVrfidHistory.setBindTime(new Date());
        stoVrfidHistoryService.saveOrUpdate(stoVrfidHistory);
    }

    /**
     * 刷新发运单开始发运时间
     * @param stoStockOutPlan
     */
    private void updateTransportOrder(StoStockOutPlan stoStockOutPlan) {
        String transportOrderNo = stoStockOutPlan.getTransportOrderNo();
        QueryWrapper<StoTransportOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TRANSPORT_ORDER_NO", transportOrderNo);
        StoTransportOrder stoTransportOrder = stoTransportOrderService.getOne(queryWrapper);
        if(stoTransportOrder != null){
            stoTransportOrder.setStartTransportTime(new Date());
            stoTransportOrder.setOrderState(2);
            stoTransportOrderService.saveOrUpdate(stoTransportOrder);
        }
    }

    /**
     * 解冻车辆
     * @param vin
     */
    private void unFrozenVehicle(String vin) {
        QueryWrapper<VehicleOffline> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("VIN", vin);
        VehicleOffline vehicleOffline2 = vehicleOfflineService.getOne(queryWrapper2);
        if (vehicleOffline2 != null) {
            vehicleOffline2.setVehicleIsFrozen(0);
            vehicleOffline2.setIsLock(0);
            vehicleOfflineService.saveOrUpdate(vehicleOffline2);

            QueryWrapper<PmsVehicleStock> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("VIN", vehicleOffline2.getVin());
            PmsVehicleStock vehicleStock = vehicleStockMService.getOne(queryWrapper);
            if (vehicleStock != null) {
                vehicleStock.setVehicleIsFrozen("0");
                vehicleStockMService.saveOrUpdate(vehicleStock);
            }

            //新增车辆解冻历史
            vehicleStockMService.newFrozenHis("0", null, vehicleOffline2.getVin(),2);
        }
    }
}
